package com.qef.com.mmovie.di;

import com.qef.com.mmovie.api.ApiSever;
import com.qef.com.mmovie.api.DowLoadServer;
import com.qef.com.mmovie.api.LaughImgServer;
import com.qef.com.mmovie.api.TopVieddoServer;
import com.qef.com.mmovie.api.UrlManager;

import javax.inject.Inject;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by guangjiego on 2017/3/22.
 */

public class RetrofitHelper {
    OkHttpClient client;
    private ApiSever apiSever;
    private TopVieddoServer mTopVieddoServer;
    private LaughImgServer laughImgServer;
    private DowLoadServer dowLoadServer;

    @Inject
    public RetrofitHelper(OkHttpClient client) {
        this.client = client;
        init();
    }

    private void init() {
        apiSever = getApiService(UrlManager.KAIYAN_HOST, ApiSever.class);
        mTopVieddoServer = getApiService(UrlManager.TOPVideo_HOST, TopVieddoServer.class);
        laughImgServer = getApiService(UrlManager.V_VIDEO_HOST, LaughImgServer.class);
        dowLoadServer = getApiService(UrlManager.QINIU_HOST, DowLoadServer.class);
    }

    private <T> T getApiService(String kaiyanHost, Class<T> apiSeverClass) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(kaiyanHost)
                .client(client).addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
        return retrofit.create(apiSeverClass);
    }

    public ApiSever getApiSever() {
        return apiSever;
    }

    public TopVieddoServer getTopVieddoServer() {
        return mTopVieddoServer;
    }

    public LaughImgServer getLaughImgServer() {
        return laughImgServer;
    }

    public DowLoadServer getDowLoadServer() {
        return dowLoadServer;
    }
}
